package com.itheima.health.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.PermissionDao;
import com.itheima.health.dao.RoleDao;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.pojo.Permission;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import com.itheima.health.utils.DateUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

/**
 * @author ：xu
 * @date ：Created in 2019/7/1
 * @description ：UserService实现类
 * @version: 1.0
 */
@Service
@Slf4j
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Autowired
    private PermissionDao permissionDao;

    /**
     * UserService实现类
     * @param userName
     * @return
     */
    @Override
    public User findByUserName(String userName) {

        //从数据库读取信息
        log.debug("service_provide...从数据库读取用户信息..findByUsername:{},p:{}",userName);
        //调用dao通过userName查询用户信息
        User user = userDao.findByUserName(userName);
        if (user == null){
            return null;
        }
        //调用roleDao获取角色列表
        Set<Role> roleSet = roleDao.findByUserId(user.getId());
        //遍历
        for (Role role : roleSet) {
            //调用permissionDao获取角色的权限列表
            Set<Permission> permissionSet = permissionDao.findByRoleId(role.getId());
            if (permissionSet != null && permissionSet.size()>0){
                role.setPermissions(permissionSet);
            }
            user.setRoles(roleSet);
        }
        return user;
    }

    /**
     * 添加用户
     * @param user
     * @param roleIds
     */
    @Override
    @Transactional
    public void add(User user, Integer[] roleIds) {
        //调用dao保存
        //添加用户
        log.debug("添加用户");
        userDao.add(user);
        for (Integer roleId : roleIds) {
            Map map = new HashMap();
            map.put("user_id",user.getId());
            map.put("role_id",roleId);
            userDao.addUserAndRole(map);
            log.debug("添加角色信息");
        }
    }

    /**
     * 分页查询
     * @param currentPage 当前页
     * @param pageSize 默认条数
     * @param queryString 查询条件
     * @return
     */
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        //调用分页插件  开始分页
        PageHelper.startPage(currentPage,pageSize);
        Page<User> page = userDao.selectByCondition(queryString);
        //封装pageResult
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 通过id查询用户信息
     * @param id
     * @return
     */
    @Override
    public User findById(Integer id) {
        return userDao.findById(id);
    }

    /**
     * 通过用户id查询中间表获取用户的角色信息
     * @param id
     * @return
     */
    @Override
    public List<Integer> findRoleIdsByUserId(Integer id) {
        return userDao.findRoleIdsByUserId(id);
    }

    /**
     * 编辑用户
     * @param user
     * @param roleIds
     */
    @Override
    @Transactional
    public void editByUserId(User user, Integer[] roleIds) {
        //调用userDao保存用户信息
        userDao.editUserById(user);
        //删除中间表信息
        userDao.deleteRoleIds(user.getId());
        for (Integer roleId : roleIds) {
            Map map = new HashMap();
            map.put("user_id",user.getId());
            map.put("role_id",roleId);
            userDao.addUserAndRole(map);
        }
    }

    /**
     * 通过id删除用户
     * @param id
     */
    @Override
    @Transactional
    public void deleteUserById(Integer id) {
        //先删除中间表数据
        userDao.deleteRoleIds(id);
        //再根据用户id删除用户数据
        userDao.deleteUserByUserId(id);
    }
}
